Python OpenCV4趣味应用系列(三)

您所在的位置:网站首页 opencv 最小外接矩形 Python OpenCV4趣味应用系列(三)

Python OpenCV4趣味应用系列(三)

2023-03-22 09:37| 来源: 网络整理| 查看: 265

import cv2 as cvimport numpy as np

# Create an imager = 100src = np.zeros((4*r, 4*r), dtype=np.uint8)

# Create a sequence of points to make a contourvert = [None]*6vert[0] = (3*r//2, int(1.34*r))vert[1] = (1*r, 2*r)vert[2] = (3*r//2, int(2.866*r))vert[3] = (5*r//2, int(2.866*r))vert[4] = (3*r, 2*r)vert[5] = (5*r//2, int(1.34*r))# Draw it in srcfor i in range(6): cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3)

cv.imshow("src", src)

# Get the contours_, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

# Calculate the distances to the contourraw_dist = np.empty(src.shape, dtype=np.float32)for i in range(src.shape[0]): for j in range(src.shape[1]): raw_dist[i,j] = cv.pointPolygonTest(contours[1], (j,i), True)

# 获取最大值即内接圆半径,中心点坐标minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist)minVal = abs(minVal)maxVal = abs(maxVal)

result = cv.cvtColor(src,cv.COLOR_GRAY2BGR)

center,radius = cv.minEnclosingCircle(contours[0])cv.circle(result,(int(center[0]),int(center[1])),int(radius),(0,0,255),2)

cv.circle(result,maxDistPt, np.int(maxVal),(0,255,0), 2, cv.LINE_8, 0)cv.imshow('result', result)

cv.waitKey(0)cv.destroyAllWindows()



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3